package com.boarsoft.boar.code.bean.biz.impl;

import java.util.List;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import com.boarsoft.boar.code.bean.biz.BeanMappingBiz;
import com.boarsoft.boar.code.entity.BeanMapping;
import com.boarsoft.hibernate.biz.SimpleBizImpl;

@Component("beanMappingBiz")
public class BeanMappingBizImpl extends SimpleBizImpl implements BeanMappingBiz {
	@Transactional
	@Override
	public String save(BeanMapping beanMapping) {
		return (String) this.dao.save(beanMapping);
	}

	@Transactional(readOnly = true)
	@Override
	public String getBeanMappingId(BeanMapping beanMapping) {
		StringBuilder hql = new StringBuilder();
		hql.append(" codeId = '").append(beanMapping.getCodeId());
		hql.append("' and key = '").append(beanMapping.getKey());
		hql.append("' and fromType = '").append(beanMapping.getFromType());
		hql.append("' and fromObj = '").append(beanMapping.getFromObj());
		hql.append("' and fromProp = '").append(beanMapping.getFromProp());
		hql.append("' and toType = '").append(beanMapping.getToType());
		hql.append("' and toObj = '").append(beanMapping.getToObj());
		hql.append("' and toProp = '").append(beanMapping.getToProp()).append("'");
		hql.append(" order by r.id");
		log.info(hql.toString());
		List<BeanMapping> beanMappingList = this.dao.list("select r from BeanMapping r where ".concat(hql.toString()));
		if (CollectionUtils.isEmpty(beanMappingList)) {
			return null;
		}
		return beanMappingList.get(0).getId();
	}

	/**
	 * 修改映射配置：/code/mapping/update.do
	 */
	@Override
	@Transactional
	public void update(BeanMapping beanMapping) {

		/*
		 * StringBuilder hql = new StringBuilder();
		 * 
		 * hql.append(" update BeanMapping set fromProp = '").append(fromProp);
		 * hql.append("' , fromPropType = '").append(fromPropType);
		 * hql.append("' , fromObj = '").append(fromObj);
		 * 
		 * if (Strings.isNotEmpty(converter)) {
		 * hql.append("' , converter = '").append(converter); }
		 * hql.append("' where id = '").append(id).append("'");
		 * log.info(hql.toString());
		 * 
		 * this.dao.executeUpdate(hql.toString());
		 */
		this.dao.update(beanMapping);

	}

	/**
	 * 启停映射配置：/code/mapping/toggle.do
	 */
	@Override
	@Transactional
	public void toggle(String id, boolean enable) {

		// update svc_conv_detail set tValidFlag = '1' where cId = id;
		StringBuilder sql = new StringBuilder();
		sql.append(" update BeanMapping set status = '");

		if (enable) {
			sql.append(1);
		} else {
			sql.append(0);
		}
		sql.append("' where id = '").append(id).append("'");

		log.info(sql.toString());

		this.dao.executeUpdate(sql.toString());
	}

	/**
	 * 删除映射配置：/code/mapping/delete.do
	 * 
	 * @param id
	 *            id
	 */
	@Override
	@Transactional
	public void delete(String id) {
		String w = String.format("and id='%s'", id);
		BeanMapping beanMapping = this.dao.find(BeanMapping.class, w);
		if (beanMapping != null) {
			log.info(beanMapping.toString());
			this.dao.delete(beanMapping);
		} else {
			log.info("删除数据不存在！");
		}
	}

	@Transactional(readOnly = true)
	@Override
	public List<BeanMapping> listFromCodeId(String codeId, String type) {
		String w = String.format("and cCodeId='%s' and vKey like 'mac/flow/WrapHandler/%s%%'", codeId, type);
		return dao.list(BeanMapping.class, w);
	}

	@Transactional(readOnly = true)
	@Override
	public List<BeanMapping> list(String id, String key) {
		StringBuilder sb = new StringBuilder();
		sb.append(" from BeanMapping r where r.codeId = '").append(id).append("'");
		sb.append(" and key = '").append(key).append("'").append(" order by r.id");
		log.info(sb.toString());
		return this.dao.list("select r".concat(sb.toString()));
	}

	@Override
	@Transactional
	public void deleteByType(String code) {
		/*Query<BeanMapping> qry = this.dao.createQuery(BeanMapping.class, "from BeanMapping where key like '?%'");
		qry.setParameter(0, code);*/
		String sb = " and key like '%" +
				code + "%'";
		List<BeanMapping> beanMappingList = this.dao.list(BeanMapping.class, sb);
		if (!CollectionUtils.isEmpty(beanMappingList)) {
			for (BeanMapping beanMapping : beanMappingList) {
				this.dao.delete(beanMapping);
			}
		}
	}
}
